/***************************************************************************
*   Copyright (C) 2005 by Alexandr Nemish   *
*   atlanter@gmail.com   *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#ifndef RENDERER_H
#define RENDERER_H

#include <qstringlist.h>
#include <qfont.h>
#include <qstring.h>
#include <qobject.h>
#include <qsize.h>
#include <vector>
class QStringList;

/**
*   \brief Renders input text into list of word wrapped strings
*	\author Alexandr Nemish <anemish@gmail.com>
*/
class Renderer : public QObject
{
	Q_OBJECT
public:
	Renderer();
	~Renderer();

	///	\brief Loads and renders list of strings
	void load(const QStringList & list);
	///	\brief Renders loaded list of strings
	void render();
	/// \brief Clears all rendered data
	void clear();
	/// \brief Draws page
	void drawPage(QPainter & paint, QRect rect, int pageNumber);
	//Getters
	/// \brief Returns whether the text is empty
	bool isEmpty() const { return m_text.isEmpty(); };
	/// \brief Returns current font
	QFont font() const { return m_font; }
	/// \brief Returns current paragraph offset in pixels
	int paraOffset() const { return m_paraOffset; }
	/// \brief Returns the number of pages
	int pageCount() const { return m_pageCount; }
	/// \brief Returns page size in pixels
	QSize pageSize() const { return m_pageSize; }
	//Setters
	/// \brief Sets current font
	void setFont(const QFont & font);
	/// \brief Sets current paragraph offset in pixels
	void setParaOffset(int offset);
	/// \brief Sets size of a page
	void setPageSize(const QSize & size);

private:
	typedef QStringList::const_iterator TStringIter;
	//make it non-copyable		
	Renderer(const Renderer &);
	Renderer & operator = (const Renderer &);
	/// \brief Renders input line into list of wrapped lines
	void parseString(const QString & string);
	/// \brief Returns width of string in pixels
	int width(const QString & a_string) const;
	void addLine(const QString & a_string);
	int wordAt(const QString & str, int len);
	QString getWord(const QString & str, int idx);
	/// \brief Draws justified string
	void drawLine(QPainter& paint, int x, int y, const QString&);

	QStringList m_text;
	QStringList m_lines;
	int m_pageCount;
	int m_linesPerPage;
	int m_paraOffset;
	QFont m_font;
	std::auto_ptr<QFontMetrics> m_fontMetrics;
	QSize m_pageSize;
	bool m_isStartAdded;
};

#endif
